YUI.add('wfsharepanel', function (Y) {
    var panel, bb,model,
    	srcNode = Y.one('#share-panel'),
    	shareInput = srcNode.one('#shared-link');
    
    shareInput.on('click',function(e){
    	this.select();
    });
    
    var buttonGroupRadio = new Y.ButtonGroup({
        srcNodes: Y.all('.share-item-radio'),
        type: 'radio'
    });
    
    function setShareInput(){
    	Y.log(window.location.href);
    	var d = new Date();
    	if(window.location.href.indexOf('8888') == -1){
    		shareInput.set('value','http://www.uxu123.com/workflow.html?random='+ d.getTime() + '#item=' + model.get('id') + '&shared=true');
    	}else{
    		shareInput.set('value','http://127.0.0.1:8888/workflow.html?random='+ d.getTime() + '#item=' + model.get('id') + '&shared=true');
    	}
    }
    
    buttonGroupRadio.on('selectionChange', function(e){
        var st = this.getSelectedButtons()[0].getNode().get('id');
        var ss = st.split('-');
        var sstt;
        if(ss.length == 4){
        	sstt = 'public-editable';
        }else{
        	sstt = ss[0];
        }
        if('public' == sstt || 'public-editable' == sstt){
        	setShareInput();
        }else{
        	shareInput.set('value','');
        }
        if(model){
        	model.set('shareType',sstt).save();
        }
    });
    
    function selectButton(st){
    	if(st && st.length > 0){
    		;
    	}else{
    		st = 'none';
    	}
    	var buttons = buttonGroupRadio.getButtons();
    	Y.each(buttons,function(b){
    		b.unselect();
    		if(b.getNode().get('id') == st + '-share-radio'){
    			b.select();
    		}
    	});
        if('public' == st || 'public-editable' == st){
        	setShareInput();
        }else{
        	shareInput.set('value','');
        }
    }
    
	function showPanel() {
	    panel.show();
	    bb.transition({
	        duration: 0.5,
	        top     : '80px'
	    });
	}
	
	function hidePanel() {
	    bb.transition({
	        duration: 0.5,
	        top     : '-300px'
	    }, function () {
	        panel.hide();
	    });
	}
	
	panel = new Y.Panel({
	    srcNode: srcNode,
	    width  : 530,
	    xy     : [300, -300],
	    zIndex : 9999,
	    modal  : true,
	    visible: false,
	    render : true,
	    centered : true,
	    buttons: [
	        {
	            value  : '关闭',
	            section: 'footer',
	            action : function (e) {
	                e.preventDefault();
	                model = null;
	                hidePanel();
	            }
	        }
	    ]
	});
	
	bb = panel.get('boundingBox');
	Y.showSharePanel =  function(){
		showPanel();
		if(Y.sharedMode){
			var btns = buttonGroupRadio.getButtons();
			Y.each(btns,function(b){
				b.disable();
			});
		}else{
			model = Y.Wfutils.getModelById(Y.lastClickedItemId);
			if(model){
				selectButton(model.get('shareType'));
			}else{
				selectButton('none');
			}
		}
	};
	
},'0.0.1',{requires:['transition', 'panel','button']});